package com.niiwoo.civet.mobile.lanmao.handler;/**
 * Created by DELL on 2017/12/7.
 */

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.civet.account.dto.request.WithdrawNotifyRequestDTO;
import com.niiwoo.civet.account.dto.response.WithdrawNotifyResponseDTO;
import com.niiwoo.civet.account.enums.WithdrawEndTypeEnum;
import com.niiwoo.civet.account.service.WithdrawDubboService;
import com.niiwoo.tripod.lanmao.enums.LanMaoGatewayServiceNameEnum;
import com.niiwoo.tripod.lanmao.enums.WithdrawFormEnum;
import com.niiwoo.tripod.lanmao.enums.WithdrawStatusEnum;
import com.niiwoo.tripod.lanmao.notify.WithdrawNotify;
import com.niiwoo.tripod.provider.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 申请提现异步回调通知处理类
 *
 * @author xiaoyingping
 * @create 2017-12-07 10:59
 **/
@Component
@Slf4j
public class WithdrawNotifyHandler implements LanMaoNotifyHandler {

    @Reference(version = "1.0.0")
    private WithdrawDubboService withdrawDubboService;

    @Override
    public boolean supports(String serviceName) {
        return LanMaoGatewayServiceNameEnum.WITHDRAW.name().equals(serviceName);
    }

    @Override
    public HandleResult handle(JSONObject json) {
        if (json == null) {
            log.error("提现回调Handler,json is null");
        }
        log.info("提现回调Handler,requestJson:{}", json.toJSONString());
        WithdrawNotify notify = JSON.parseObject(json.toJSONString(), WithdrawNotify.class);
        WithdrawNotifyRequestDTO withdrawNotifyRequestDTO = new WithdrawNotifyRequestDTO();
        withdrawNotifyRequestDTO.setWithdrawStatus(notify.getWithdrawStatus().getValue());
        withdrawNotifyRequestDTO.setRequestNo(notify.getRequestNo());
        withdrawNotifyRequestDTO.setAmount(notify.getAmount());
        withdrawNotifyRequestDTO.setPlatformUserNo(notify.getPlatformUserNo());
        withdrawNotifyRequestDTO.setFloatAmount(notify.getFloatAmount());
        withdrawNotifyRequestDTO.setOperateUserId(0L);

        HandleResult result = new HandleResult();
        result.setRequestNo(withdrawNotifyRequestDTO.getRequestNo());
        if (notify.isSuccess()) {
            try {
                //直连提现
                if(WithdrawFormEnum.IMMEDIATE == WithdrawFormEnum.valueOf(notify.getWithdrawForm())){
                    if(notify.getWithdrawStatus() == WithdrawStatusEnum.ACCEPT){
                        //提现受理成功
                        WithdrawNotifyResponseDTO responseDTO= withdrawDubboService.processSubmitWithdrawPlateNotify(withdrawNotifyRequestDTO);
                        result.setData(responseDTO);
                    }
                    //提现出款成功
                    else if(notify.getWithdrawStatus() == WithdrawStatusEnum.SUCCESS){
                        withdrawNotifyRequestDTO.setEndTypeEnum(WithdrawEndTypeEnum.SYSTEM_AUTO);
                        withdrawDubboService.withdrawPaymentSuccess(withdrawNotifyRequestDTO);
                    }
                    //受理失败可以确认提现失败。否则只能以资金回退充值状态为准
                    else if(notify.getWithdrawStatus() == WithdrawStatusEnum.ACCEPT_FAIL){
                        withdrawNotifyRequestDTO.setEndTypeEnum(WithdrawEndTypeEnum.SYSTEM_AUTO);
                        withdrawDubboService.withdrawPaymentFail(withdrawNotifyRequestDTO);
                    }
                    //提现失败仅记录日志
                    else if(notify.getWithdrawStatus() == WithdrawStatusEnum.FAIL){
                        log.info("提现出款失败：{}",notify.getRequestNo());
                    }
                    result.setNeedAck(true);
                    log.info("请求回调处理成功###data={}", JSON.toJSONString(result));
                    return result;
                }
                //待确认提现
                else if(WithdrawFormEnum.CONFIRMED == WithdrawFormEnum.valueOf(notify.getWithdrawForm())){
                    if(notify.getWithdrawStatus() == WithdrawStatusEnum.CONFIRMING){
                        //提现受理成功
                        WithdrawNotifyResponseDTO responseDTO= withdrawDubboService.processSubmitWithdrawNotify(withdrawNotifyRequestDTO);
                        result.setData(responseDTO);
                    }
                    //提现出款成功
                    else if(notify.getWithdrawStatus() == WithdrawStatusEnum.SUCCESS){
                        withdrawNotifyRequestDTO.setEndTypeEnum(WithdrawEndTypeEnum.SYSTEM_AUTO);
                        withdrawDubboService.withdrawPaymentSuccess(withdrawNotifyRequestDTO);
                    }
                    //受理失败可以确认提现失败
                    else if(notify.getWithdrawStatus() == WithdrawStatusEnum.ACCEPT_FAIL){
                        withdrawNotifyRequestDTO.setEndTypeEnum(WithdrawEndTypeEnum.SYSTEM_AUTO);
                        withdrawDubboService.withdrawPaymentFail(withdrawNotifyRequestDTO);
                    }
                    result.setNeedAck(true);
                    log.info("请求回调处理成功###data={}", JSON.toJSONString(result));
                    return result;
                }else{
                    throw new BizException(notify.getErrorCode(), notify.getErrorMessage());
                }
            } catch (Exception e) {
                log.error("请求回调处理失败###", e);
                throw e;
            }
        } else {
            throw new BizException(notify.getErrorCode(), notify.getErrorMessage());
        }
    }
}
